package com.cjf.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cjf.comon.BaseContext;
import com.cjf.comon.R;
import com.cjf.dto.SetmealDto;
import com.cjf.entity.Category;
import com.cjf.entity.Setmeal;
import com.cjf.entity.SetmealDish;
import com.cjf.service.CategoryService;
import com.cjf.service.SetmealDishService;
import com.cjf.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

/***
 * 套餐管理类
 */
@RestController
@RequestMapping("/setmeal")
@Slf4j
public class SetmealController {

    @Autowired
    private SetmealService setmealService;
    @Autowired
    private SetmealDishService setmealDishService;
    @Autowired
    private CategoryService categoryService;

    /***
     * 新增套餐
     * @param setmealDto
     * @return
     */
    @PostMapping
    public R<String> save(@RequestBody SetmealDto setmealDto) {
        setmealService.saveWithDish(setmealDto);
        return R.success("新增套餐成功!");
    }


    /***
     * 套餐分页查询
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    public R<Page> page(int page,int pageSize,String name) {

        Page<Setmeal> pageInfo = new Page<>(page,pageSize);

        Page<Setmeal> setmealDtoPage = new Page<>();

        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();

        //添加过滤条件
        queryWrapper.like(name != null, Setmeal::getName, name);
        //添加排序条件
        queryWrapper.orderByDesc(Setmeal::getUpdateTime);
        //执行分页查询
        setmealService.page(pageInfo, queryWrapper);

        //对象拷贝
        BeanUtils.copyProperties(pageInfo, setmealDtoPage, "records");

        List<Setmeal> records = pageInfo.getRecords();

        List<Setmeal> list = records.stream().map((item) -> {

            SetmealDto setmealDto = new SetmealDto();

            //对象拷贝 除categoryName属性 其他属性都可从Dish拷贝
            BeanUtils.copyProperties(item, setmealDto);

            Long categoryId = item.getCategoryId();//菜品分类id

            //根据id查询分类对象
            Category category = categoryService.getById(categoryId);
            //通过对象属性 查询分类名称
            String categoryName = category.getName();

            //将查询的分类名称 赋值给DishDto对象
            setmealDto.setCategoryName(categoryName);

            return setmealDto;

            //转集合操作
        }).collect(Collectors.toList());

        setmealDtoPage.setRecords(list);

        return R.success(setmealDtoPage);
    }

    /***
     * 删除套餐
     * @param ids
     * @return
     */
    @DeleteMapping
    public R<String> delete(@RequestParam List<Long> ids) {
        setmealService.removeWithDish(ids);

        return R.success("删除套餐成功！");
    }

    /***
     * 根据条件查询套餐数据
     * @param setmeal
     * @return
     */
    @GetMapping("/list")
    public R<List<Setmeal>> list(Setmeal setmeal) {

        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(setmeal.getCategoryId() != null,Setmeal::getCategoryId,setmeal.getCategoryId());
        queryWrapper.eq(setmeal.getStatus() != null,Setmeal::getStatus,setmeal.getStatus());
        List<Setmeal> list = setmealService.list(queryWrapper);
        return R.success(list);
    }


    /***
     * 根据id查询套餐信息和 套餐包含的菜品信息
     * @return
     */
    @GetMapping("/{id}")
    public R<SetmealDto> get(@PathVariable Long id) {

        SetmealDto setmealDto = setmealService.getByIdWithFlavor(id);
        return R.success(setmealDto);
    }


    /***
     * 修改套餐状态 批量修改
     * @param ids
     * @param status
     * @return
     */
    @PostMapping("/status/{status}")
    public R<String> updateStatus(@RequestParam List<Long> ids,@PathVariable Integer status) {
        List<Setmeal> new_lis = new ArrayList<>();
        for (long tmpId : ids){
            Setmeal setmeal = new Setmeal();
            setmeal.setId(tmpId);
            setmeal.setStatus(status);
            setmeal.setUpdateUser(BaseContext.getCurrentId());
            setmeal.setUpdateTime(LocalDateTime.now());
            new_lis.add(setmeal);
        }
        setmealService.updateBatchById(new_lis);
        return R.success("套餐状态修改成功！");
    }

}
